// pages/settlementPage/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        addressList: [],
        aId: 0,
        orderList: [],
        totalPrice: 0
    },

    radioChange(e) {
        const newId = parseInt(e.detail.value)
        this.setData({
            aId: newId
        })
    },

    createOrder() {
        if (this.data.aId !== 0) {
            const productId = []
            const specId = []
            const count = []
            const PSId = []
            this.data.orderList.forEach(item => {
                productId.push(item.productId)
                specId.push(item.specId)
                PSId.push(item.productId + "-" + item.specId)
                count.push(item.count)
            })
            const time = Date.parse(new Date())
            const orderName = this.data.orderList[0].title + (this.data.orderList.length > 1 ? "等" + this.data.orderList + "件商品" : "")
            wx.request({
                url: 'https://mbp.jyccloud.cn/mbp/addOrder',
                method: "POST",
                data: JSON.stringify({
                    orderId: time.toString(),
                    orderName: orderName,
                    openId: this.data.orderList[0].openId,
                    productId: productId.join(),
                    specId: specId.join(),
                    totalPrice: this.data.totalPrice,
                    count: count.join(),
                    addressId: this.data.aId,
                    PSId: PSId
                }),
                success: (r => {
                    wx.requestPayment({
                        ...r.data,
                        success: function (res) {
                            wx.request({
                                url: 'https://mbp.jyccloud.cn/mbp/editPayOrder',
                                method: "POST",
                                data: JSON.stringify({
                                    orderId: time.toString()
                                }),
                                success: (r => {
                                    wx.showToast({
                                        title: '下单成功！',
                                        icon: 'success',
                                        duration: 2000
                                    })
                                })
                            })
                        },
                        fail: function (res) {
                            wx.showToast({
                                title: '支付未完成，订单将为您保留两小时，请尽快支付！',
                                icon: 'none',
                                duration: 2000
                            })
                        },
                        complete: function (res) {
                            wx.switchTab({
                                url: '../../pages/home/index'
                            })
                        }
                    })
                    // wx.setStorageSync('cart', r.data)
                })
            })
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const openId = wx.getStorageSync('openId')
        const orderList = wx.getStorageSync('order')
        let totalPrice = 0
        orderList.forEach(r =>
            totalPrice += r.price
        )
        wx.request({
            url: 'https://mbp.jyccloud.cn/mbp/getUserAddress',
            method: "POST",
            data: JSON.stringify({
                openId: openId
            }),
            success: r => {
                this.setData({
                    addressList: r.data,
                    aId: r.data[0]["addressId"]
                })
            }
        })
        this.setData({
            orderList: orderList,
            totalPrice: totalPrice
        })
        console.log("页面加载了")
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})